package com.jacars.biz.user.dao.service.impl;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jacars.biz.user.dao.mapper.JsptRoleMapper;
import com.jacars.biz.user.dao.mapper.JsptRoleMenuMapper;
import com.jacars.biz.user.dao.mapper.JsptUserRoleMapper;
import com.jacars.biz.user.dao.service.JsptRoleService;
import com.jacars.common.lock.DistributedLock;
import com.jacars.common.api.PageResult;
import com.jacars.common.api.Result;
import com.jacars.common.model.JsptRole;
import com.jacars.common.service.impl.SuperServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 */
@Slf4j
@Service
public class JsptRoleServiceImpl extends SuperServiceImpl<JsptRoleMapper, JsptRole> implements JsptRoleService {
    private final static String LOCK_KEY_ROLECODE = "rolecode:";

    @Resource
    private JsptUserRoleMapper userRoleMapper;

    @Resource
    private JsptRoleMenuMapper roleMenuMapper;

    @Autowired
    private DistributedLock lock;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRole(JsptRole jsptRole) throws Exception {
        String roleCode = jsptRole.getRoleAlias();
        super.saveIdempotency(jsptRole, lock
                , LOCK_KEY_ROLECODE + roleCode, new QueryWrapper<JsptRole>().eq("role_alias", roleCode), "角色别名已存在");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRole(Long id) {
        baseMapper.deleteById(id);
        roleMenuMapper.delete(id, null);
        userRoleMapper.deleteUserRole(null, id);
    }

    @Override
    public PageResult<JsptRole> findRoles(Map<String, Object> params) {
        Integer curPage = MapUtils.getInteger(params, "page");
        Integer limit = MapUtils.getInteger(params, "limit");
        Page<JsptRole> page = new Page<>(curPage == null ? 0 : curPage, limit == null ? -1 : limit);
        List<JsptRole> list = baseMapper.findList(page, params);
        return PageResult.<JsptRole>builder().data(list).code(0).count(page.getTotal()).build();
    }

    @Override
    @Transactional
    public Result saveOrUpdateRole(JsptRole jsptRole) throws Exception {
        if (jsptRole.getId() == null) {
            this.saveRole(jsptRole);
        } else {
            baseMapper.updateById(jsptRole);
        }
        return Result.succeed("操作成功");
    }

    @Override
    public List<JsptRole> findAll() {
        return baseMapper.findAll();
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public void saveTenantRole(JsptRole jsptRole) {
        this.save(jsptRole);
    }

}
